/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ik.pbs.Correo;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Malkavian
 */
public class SendMail {

    private static final String SmtpServer = "smtp.gmail.com";
    private static final String ServerPort = "465";

    public SendMail() {
    }

    public boolean sendSSLMessage(final Correo correo) {

        try {
            boolean debug = false;
            Properties props = new Properties();
            props.put("mail.smtp.host", SmtpServer);
            props.put("mail.smtp.auth", "true");
            props.put("mail.debug", "true");
            props.put("mail.smtp.port", ServerPort);
            props.put("mail.smtp.ssl.enable", "true");


            Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(correo.getDesde(), correo.getContraseña());
                }
            });

            session.setDebug(debug);
            Message msg = new MimeMessage(session);

            InternetAddress addressFrom = new InternetAddress(correo.getDesde());

            msg.setFrom(addressFrom);
            String[] hacia = correo.getHacia();
            InternetAddress[] addressTo = new InternetAddress[hacia.length];



            for (int i = 0; i < hacia.length; i++) {

                addressTo[i] = new InternetAddress(hacia[i]);

            }

            msg.setRecipients(Message.RecipientType.TO, addressTo);

            //Setting the SUbject and Contect Type
            msg.setSubject(correo.getAsunto());
            msg.setContent(correo.getContenido(), "text/plain");
            Transport.send(msg);

            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }
    
//    public static void main(String[] args) {
//        Correo corr = new Correo();
//        
//        corr.setAsunto("Prueba");
//        corr.setContenido("Esto es una prueba");
//        corr.setContraseña("prietovillar");
//        corr.setDesde("twlster019@gmail.com");
//        String[] hacia = new String[1];
//        
//        hacia[0]="twlster.mk@gmail.com";
//        
//        corr.setHacia(hacia);
//        
//        SendMail m = new SendMail();
//        
//        m.sendSSLMessage(corr);
//    }
}
